|
1
|
|
|
/** global: validator */ |
|
2
|
|
|
import { Validator } from '@trojs/validator'; |
|
3
|
|
|
import errorSchema from './schemas/error.js'; |
|
4
|
|
|
|
|
5
|
9 |
|
const validator = new Validator(errorSchema); |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @typedef {Object} ErrorValues |
|
9
|
|
|
* @property {string} name |
|
10
|
|
|
* @property {string} message |
|
11
|
|
|
* @property {any} value |
|
12
|
|
|
* @property {number} status |
|
13
|
|
|
* @property {Function|Promise} type |
|
14
|
|
|
* @property {Date} date |
|
15
|
|
|
* @property {object} me |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
export default (error = Error) => |
|
|
|
|
|
|
19
|
81 |
|
class AppError extends error { |
|
20
|
|
|
/** |
|
21
|
|
|
* Set the error values |
|
22
|
|
|
* |
|
23
|
|
|
* @param {object} error |
|
24
|
|
|
* @param {any=} error.value |
|
25
|
|
|
* @param {object=} error.type |
|
26
|
|
|
* @param {string} error.message |
|
27
|
|
|
* @param {object=} error.me |
|
28
|
|
|
*/ |
|
29
|
|
|
constructor({ value = null, type = null, message, me = null }) { |
|
30
|
27 |
|
super(message); |
|
31
|
|
|
|
|
32
|
27 |
|
if (Error.captureStackTrace) { |
|
33
|
27 |
|
Error.captureStackTrace(this, AppError); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
27 |
|
this.setValues({ value, type, me }); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the error name |
|
41
|
|
|
* |
|
42
|
|
|
* @return {string} |
|
43
|
|
|
*/ |
|
44
|
|
|
get name() { |
|
45
|
30 |
|
return 'AppError'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the error status |
|
50
|
|
|
* |
|
51
|
|
|
* @return {number} |
|
52
|
|
|
*/ |
|
53
|
|
|
get errorStatus() { |
|
54
|
18 |
|
return 500; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the error status, or 500 if the error is invalid |
|
59
|
|
|
* |
|
60
|
|
|
* @return {number} |
|
61
|
|
|
*/ |
|
62
|
|
|
get status() { |
|
63
|
23 |
|
return this.hasErrors ? 500 : this.errorStatus; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the error values |
|
68
|
|
|
* |
|
69
|
|
|
* @param {object} data |
|
70
|
|
|
* @param {any} data.value |
|
71
|
|
|
* @param {object} data.type |
|
72
|
|
|
* @param {object} data.me |
|
73
|
|
|
*/ |
|
74
|
|
|
setValues({ value, type, me }) { |
|
75
|
27 |
|
this.value = value; |
|
76
|
27 |
|
this.type = type; |
|
77
|
27 |
|
this.me = me; |
|
78
|
27 |
|
this.date = new Date(); |
|
79
|
|
|
|
|
80
|
27 |
|
if (!this.validate()) { |
|
81
|
9 |
|
this.type = Error; |
|
82
|
9 |
|
this.message = 'Invalid error'; |
|
83
|
9 |
|
this.value = { errors: validator.errors, values: this.values }; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Check if the error has errors |
|
89
|
|
|
* |
|
90
|
|
|
* @return {boolean} |
|
91
|
|
|
*/ |
|
92
|
|
|
get hasErrors() { |
|
93
|
23 |
|
return validator.errors.length > 0; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the error values |
|
98
|
|
|
* |
|
99
|
|
|
* @return {ErrorValues} |
|
100
|
|
|
*/ |
|
101
|
|
|
get values() { |
|
102
|
36 |
|
return { |
|
103
|
|
|
name: this.name, |
|
104
|
|
|
message: this.message, |
|
105
|
|
|
value: this.value, |
|
106
|
|
|
status: this.errorStatus, |
|
107
|
|
|
type: this.type, |
|
108
|
|
|
date: this.date, |
|
109
|
|
|
me: this.me, |
|
110
|
|
|
}; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Validate the error values |
|
115
|
|
|
* |
|
116
|
|
|
* @return {boolean} |
|
117
|
|
|
*/ |
|
118
|
|
|
validate() { |
|
119
|
27 |
|
return validator.validate(this.values); |
|
120
|
|
|
} |
|
121
|
|
|
}; |
|
122
|
|
|
|